package com.huiquan.body.controller;

import com.huiquan.body.domain.BodyOrgan;
import com.huiquan.body.domain.BodyOrganSymptom;
import com.huiquan.body.service.BodyService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.DateUtils;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lichao email:lichao@witspring.com
 * @Description:
 * @Date 2018/2/24 11:03
 * @since Ver V
 */
@Controller
@RequestMapping("/body")
public class BodyController {

    @Autowired
    private BodyService bodyService;

    /**
     * 进入器官分级界面
     *
     * @param req
     * @return
     */
    @RequestMapping("/organ_list.htm")
    public ModelAndView organList(HttpServletRequest req) {
        String tabid = req.getParameter("tabid");
        Map<String, Object> pageParam;
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        }
        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        return bodyService.organList(start);
    }

    /**
     * 编辑器官
     *
     * @param req
     * @param image
     * @return
     */
    @ResponseBody
    @RequestMapping("/editOrgan.do")
    public ReturnData editOrgan(HttpServletRequest req,
                                @RequestParam(required = false) MultipartFile image) {
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String parentId = req.getParameter("parentId");
        String rank = req.getParameter("rank");
        String picturePre = req.getParameter("picturePre");
        BodyOrgan bodyOrgan = new BodyOrgan(name, Integer.valueOf(sex), Long.parseLong(parentId), rank);
        if (StringUtils.isNotEmpty(picturePre)) {
            bodyOrgan.setPicturePre(picturePre);
            bodyOrgan.setPicture(picturePre.concat("_").concat(DateUtils.convertDateFormat(new Date(), DateUtils.DateFormat.SIMPLE)).concat(".png"));
        }
        String id = req.getParameter("id");
        return bodyService.editOrgan(image, bodyOrgan, id);
    }

    /**
     * 删除器官
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteOrgan.do")
    public ReturnData deleteOrgan(String id) {
        return bodyService.deleteOrgan(Long.parseLong(id));
    }

    /**
     * 进入人体图症状界面
     *
     * @param req
     * @return
     */
    @RequestMapping("/symptom_list.htm")
    public ModelAndView symptomList(HttpServletRequest req) {
        String tabid = req.getParameter("tabid");
        Map<String, Object> pageParam;
        if (tabid != null && !tabid.isEmpty()) {
            pageParam = new HashMap<>();
            pageParam.put("tabid", tabid);
            req.getSession().setAttribute("pageParam", pageParam);
        } else {
            pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
        }
        // 获取页面参数，如果request中有值则修改session，否则从session中拿值
        String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
        return bodyService.symptomList(start);
    }

    /**
     * 编辑器官
     *
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping("/editOrganSymptom.do")
    public ReturnData editOrganSymptom(HttpServletRequest req) {
        String organId = req.getParameter("organId");
        String symptom = req.getParameter("symptom");
        String rank = req.getParameter("rank");
        BodyOrganSymptom bodyOrganSymptom = new BodyOrganSymptom(Long.parseLong(organId), symptom, Integer.valueOf(rank));
        String id = req.getParameter("id");
        return bodyService.editOrganSymptom(bodyOrganSymptom, id);
    }

    /**
     * 删除器官症状
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteOrganSymptom.do")
    public ReturnData deleteOrganSymptom(String id) {
        return bodyService.deleteOrganSymptom(Long.parseLong(id));
    }
}
